//
//  AYMSQLiteManager.m
//  QuickCure
//
//  Created by 徐绩 on 16/5/9.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMSQLiteManager.h"

static FMDatabase *_db;
@implementation AYMSQLiteManager

//+(instancetype)sharedManager{
//
//    static AYMSQLiteManager *instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        instance = [[self alloc]init];
//    });
//    return instance;
//}

+(void)initialize{
    
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"quickCure.db"];
    Log(@"%@",filePath);
    _db = [[FMDatabase alloc]initWithPath:filePath];
    //打开
    if([_db open]){
        
        //创建天气表
        NSString *weatherSql = @"create table if not exists T_WEATHER ('date' primary key,'temperature' text ,'weather' text,'city' text, 'dayPictureUrl' text)";
        if([_db executeUpdate:weatherSql]){
            
            Log(@"创建天气表成功");
        }else{
            
            Log(@"创建天气表失败");
        }
        //创建关注医生表
        NSString *focusDocSql = @"create table if not exists T_FOUCDOC ('doctor_id' primary key,'doctor_name' text ,'doctor_gender' text ,'doctor_portrait' text,'flower' text, 'doctor_title_id' text, 'doctor_title_name' text ,'banner' text ,'easymob_id' text ,'doctor_attention_id' text ,'hospital_id' text ,'operation_count' text , 'hospital_name' text)";
        if([_db executeUpdate:focusDocSql]){
            
            Log(@"创建医生表成功");
        }else{
            
            Log(@"创建医生表失败");
        }
        
        //创建tumor表
        NSString *tumorSql = @"create table if not exists T_TUMOR ('doctor_id' text,'doctor_name' text ,'doctor_gender' text ,'doctor_portrait' text,'ci3_name' text, 'complication_name' text, 'doctor_title_name' text ,'banner' text ,'accuracy' text ,'operation_count' text , 'doctor_hospital_name' text,'flower' text)";
        if([_db executeUpdate:tumorSql]){
            
            Log(@"创建tumor表成功");
        }else{
            
            Log(@"创建tumor表失败");
        }
        
        //创建ci3_name表
        NSString *ci3_nameSql = @"create table if not exists T_CI3_NAME ('ci3_name' text)";
        if([_db executeUpdate:ci3_nameSql]){
            
            Log(@"创建ci3_name表成功");
        }else{
            
            Log(@"创建ci3_name表失败");
        }
        
        //创建complication_name表
        NSString *complication_nameSql = @"create table if not exists T_COMPLICATION ('complication_name' text)";
        if([_db executeUpdate:complication_nameSql]){
            
            Log(@"创建complication_nameSql表成功");
        }else{
            
            Log(@"创建complication_nameSql表失败");
        }
        
    }else{
        Log(@"数据库打开失败");
    }
}

/**
 *  插入Tumor数据
 */
+(void)insertTumorWithModel:(AYMTumorModel *)tumorModel{
    
    NSString *tumorSql = [NSString stringWithFormat:@"insert into T_TUMOR (doctor_id,doctor_name,doctor_gender,doctor_portrait,flower,ci3_name,doctor_title_name,banner,complication_name,accuracy,operation_count,doctor_hospital_name) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",tumorModel.doctor_id,tumorModel.doctor_name,tumorModel.doctor_gender,tumorModel.doctor_portrait,tumorModel.flower,tumorModel.ci3_name,tumorModel.doctor_title_name,tumorModel.banner,tumorModel.complication_name,tumorModel.accuracy,tumorModel.operation_count,tumorModel.doctor_hospital_name];
    
    if([_db executeUpdate:tumorSql]){
        Log(@"插入Tumor成功");
    }else{
        Log(@"插入Tumor失败");
    }
}

/**
 *  插入tumor->ci3_name
 */
+(void)insertTumor_ci3_nameWithModel:(AYMTumorModel *)tumorModel{

    NSString *ci3_nameSql = [NSString stringWithFormat:@"insert into T_CI3_NAME (ci3_name) values ('%@')",tumorModel.ci3_name];
    
    if([_db executeUpdate:ci3_nameSql]){
        Log(@"插入tumor->ci3_name成功");
    }else{
        Log(@"插入tumor->ci3_name失败");
    }

}
/**
 *  插入tumor->complication_name
 */
+(void)insertTumor_complication_nameWithModel:(AYMTumorModel *)tumorModel{

    NSString *complication_nameSql = [NSString stringWithFormat:@"insert into T_COMPLICATION (complication_name) values ('%@')",tumorModel.complication_name];
    
    if([_db executeUpdate:complication_nameSql]){
        Log(@"插入complication_name成功");
    }else{
        Log(@"插入complication_name失败");
    }
}


/**
 *  插入天气数据
 */
+(void)insertWeatherWithModel:(AYMWeatherModel *)weatherModel{
    
    NSString *weatherSql = [NSString stringWithFormat:@"insert into T_WEATHER (date,temperature,weather,city,dayPictureUrl) values ('%@','%@','%@','%@','%@')",weatherModel.date,weatherModel.temperature,weatherModel.weather,weatherModel.city,weatherModel.dayPictureUrl];
    
    if([_db executeUpdate:weatherSql]){
        Log(@"插入天气成功");
    }else{
        Log(@"插入天气失败");
    }
}

/**
 *  插入医生数据
 */
+(void)insertDoctorWithModel:(AYMFocusDoctorModel *)focusDocModel{

    NSString *docSql = [NSString stringWithFormat:@"insert into T_FOUCDOC (doctor_id,doctor_name,doctor_gender,doctor_portrait,flower,doctor_title_id,doctor_title_name,banner,easymob_id,doctor_attention_id,hospital_id,operation_count,hospital_name) values ('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",focusDocModel.doctor_id,focusDocModel.doctor_name,focusDocModel.doctor_gender,focusDocModel.doctor_portrait,focusDocModel.flower,focusDocModel.doctor_title_id,focusDocModel.doctor_title_name,focusDocModel.banner,focusDocModel.easymob_id,focusDocModel.doctor_attention_id,focusDocModel.hospital_id,focusDocModel.operation_count,focusDocModel.hospital_name];
    
    if([_db executeUpdate:docSql]){
        Log(@"插入医生成功");
    }else{
        Log(@"插入医生失败");
    }
}

/**
 *  查询天气数据
 */
+ (NSArray *)queryWeather{
    FMResultSet *weatherSet = [_db executeQuery:@"select * from T_WEATHER"];
    
    NSMutableArray *weatherArr = [NSMutableArray array];
    
    while (weatherSet.next) {

        NSString *date = [weatherSet stringForColumn:@"date"];
        NSString *temperature = [weatherSet stringForColumn:@"temperature"];
        NSString *weather = [weatherSet stringForColumn:@"weather"];
        NSString *city = [weatherSet stringForColumn:@"city"];
        NSString *dayPictureUrl = [weatherSet stringForColumn:@"dayPictureUrl"];
        
        AYMWeatherModel *weatherModel = [[AYMWeatherModel alloc]init];
        weatherModel.date = date;
        weatherModel.temperature = temperature;
        weatherModel.weather = weather;
        weatherModel.city = city;
        weatherModel.dayPictureUrl = dayPictureUrl;
        
        //将其添加到数组中
        [weatherArr addObject:weatherModel];
    }
    return weatherArr.copy;
}

/**
 *  查询关注医生
 */
+ (NSArray *)queryDoctor{

    FMResultSet *doctorSet = [_db executeQuery:@"select * from T_FOUCDOC"];
    
    NSMutableArray *doctorArr = [NSMutableArray array];
    
    while (doctorSet.next) {
        
        NSString *doctor_id = [doctorSet stringForColumn:@"doctor_id"];
        NSString *doctor_name = [doctorSet stringForColumn:@"doctor_name"];
        NSString *doctor_gender = [doctorSet stringForColumn:@"doctor_gender"];
        NSString *doctor_portrait = [doctorSet stringForColumn:@"doctor_portrait"];
        NSString *flower = [doctorSet stringForColumn:@"flower"];
        NSString *doctor_title_id = [doctorSet stringForColumn:@"doctor_title_id"];
        NSString *doctor_title_name = [doctorSet stringForColumn:@"doctor_title_name"];
        NSString *banner = [doctorSet stringForColumn:@"banner"];
        NSString *easymob_id = [doctorSet stringForColumn:@"easymob_id"];
        NSString *doctor_attention_id = [doctorSet stringForColumn:@"doctor_attention_id"];
        NSString *hospital_id = [doctorSet stringForColumn:@"hospital_id"];
        NSString *operation_count = [doctorSet stringForColumn:@"operation_count"];
        NSString *hospital_name = [doctorSet stringForColumn:@"hospital_name"];
        
        AYMFocusDoctorModel *focusDocModel = [[AYMFocusDoctorModel alloc]init];
        focusDocModel.doctor_id = doctor_id;
        focusDocModel.doctor_name = doctor_name;
        focusDocModel.doctor_gender = doctor_gender;
        focusDocModel.doctor_portrait = doctor_portrait;
        focusDocModel.flower = flower;
        focusDocModel.doctor_title_id = doctor_title_id;
        focusDocModel.doctor_title_name = doctor_title_name;
        focusDocModel.banner = banner;
        focusDocModel.easymob_id = easymob_id;
        focusDocModel.doctor_attention_id = doctor_attention_id;
        focusDocModel.hospital_id = hospital_id;
        focusDocModel.operation_count = operation_count;
        focusDocModel.hospital_name = hospital_name;
        
        
        //将其添加到数组中
        [doctorArr addObject:focusDocModel];
    }
    return doctorArr.copy;
}
/**
 *  查询tumor
 */
+ (NSArray *)queryTumor{
    
    FMResultSet *TumorSet = [_db executeQuery:@"select * from T_TUMOR"];
    
    NSMutableArray *TumorArr = [NSMutableArray array];
    
    while (TumorSet.next) {
        
        NSString *doctor_id = [TumorSet stringForColumn:@"doctor_id"];
        NSString *doctor_name = [TumorSet stringForColumn:@"doctor_name"];
        NSString *doctor_gender = [TumorSet stringForColumn:@"doctor_gender"];
        NSString *doctor_portrait = [TumorSet stringForColumn:@"doctor_portrait"];
        NSString *flower = [TumorSet stringForColumn:@"flower"];
        NSString *ci3_name = [TumorSet stringForColumn:@"ci3_name"];
        NSString *doctor_title_name = [TumorSet stringForColumn:@"doctor_title_name"];
        NSString *banner = [TumorSet stringForColumn:@"banner"];
        NSString *complication_name = [TumorSet stringForColumn:@"complication_name"];
        NSString *accuracy = [TumorSet stringForColumn:@"accuracy"];
        NSString *operation_count = [TumorSet stringForColumn:@"operation_count"];
        NSString *doctor_hospital_name = [TumorSet stringForColumn:@"doctor_hospital_name"];
        
        AYMTumorModel *tumorModel = [[AYMTumorModel alloc]init];
        tumorModel.doctor_id = doctor_id;
        tumorModel.doctor_name = doctor_name;
        tumorModel.doctor_gender = doctor_gender;
        tumorModel.doctor_portrait = doctor_portrait;
        tumorModel.flower = flower;
        tumorModel.doctor_title_name = doctor_title_name;
        tumorModel.banner = banner;
        tumorModel.ci3_name = ci3_name;
        tumorModel.complication_name = complication_name;
        tumorModel.accuracy = accuracy;
        tumorModel.operation_count = operation_count;
        tumorModel.doctor_hospital_name = doctor_hospital_name;
        
        
        //将其添加到数组中
        [TumorArr addObject:tumorModel];
    }
    return TumorArr.copy;
    
}
/**
 *  查询tumor->ci3_name
 */
+ (NSArray *)queryTumorCi3_name{

    FMResultSet *TumorSet = [_db executeQuery:@"select * from T_CI3_NAME"];
    
    NSMutableArray *TumorArr = [NSMutableArray array];
    
    while (TumorSet.next) {
        
        NSString *ci3_name = [TumorSet stringForColumn:@"ci3_name"];
        
        AYMTumorModel *tumorModel = [[AYMTumorModel alloc]init];
        tumorModel.ci3_name = ci3_name;
        //将其添加到数组中
        [TumorArr addObject:tumorModel];
    }
    return TumorArr.copy;
}
/**
 *  查询tumor->complication_name
 */
+ (NSArray *)queryTumorComplication_name{

    FMResultSet *TumorSet = [_db executeQuery:@"select * from T_COMPLICATION"];
    
    NSMutableArray *TumorArr = [NSMutableArray array];
    
    while (TumorSet.next) {
        
         NSString *complication_name = [TumorSet stringForColumn:@"complication_name"];
        
        AYMTumorModel *tumorModel = [[AYMTumorModel alloc]init];
        tumorModel.complication_name = complication_name;
        //将其添加到数组中
        [TumorArr addObject:tumorModel];
    }
    return TumorArr.copy;
}

/**
 *  清除缓存
 */
+(void)clearCache{

    NSTimeInterval maxTimeInterval = 60 * 60 * 24 * 7;
    //距离当前时间前7天
    NSDate *targetDate = [NSDate dateWithTimeIntervalSinceNow:-maxTimeInterval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *timeStr = [formatter stringFromDate:targetDate];
    NSString *deleteSql = [NSString stringWithFormat:@"delete from T_WEATHER where date<%@",timeStr];
    if([_db executeUpdate:deleteSql]){
        Log(@"删除成功");
    }else{
        Log(@"删除失败");
    }
}

@end
